home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / JCheckBoxMenuItem.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  9.0 KB  |  301 lines  |  [TEXT/CWIE]

  1.  /*
  2.  * @(#)JCheckBoxMenuItem.java    1.38 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.util.EventListener;
  17.  
  18. import java.awt.*;
  19. import java.awt.event.*;
  20. import java.awt.image.*;
  21.  
  22. import java.io.ObjectOutputStream;
  23. import java.io.ObjectInputStream;
  24. import java.io.IOException;
  25.  
  26. import javax.swing.plaf.*;
  27. import javax.accessibility.*;
  28.  
  29.  
  30. /**
  31.  * A menu item that can be selected or deselected. If selected, the menu
  32.  * item typically appears with a checkmark next to it. If unselected or
  33.  * deselected, the menu item appears without a checkmark. Like a regular
  34.  * menu item, a checkbox menu item can have either text or a graphic
  35.  * icon associated with it, or both.
  36.  * <p>
  37.  * Either <code>isSelected</code>/<code>setSelected</code> or 
  38.  * <code>getState</code>/<code>setState</code> can be used
  39.  * to determine/specify the menu item's selection state. (The
  40.  * Swing-standard methods are <code>isSelected</code> and
  41.  * <code>setSelected</code>. These methods work for all menus and buttons.
  42.  * The <code>getState</code> and <code>setState</code> methods exist for
  43.  * compatibility with other component sets.)
  44.  * <p>
  45.  * For the keyboard keys used by this component in the standard Look and
  46.  * Feel (L&F) renditions, see the
  47.  * <a href="doc-files/Key-Index.html#JCheckBoxMenuItem">JCheckBoxMenuItem</a> key assignments.
  48.  * <p>
  49.  * <strong>Warning:</strong>
  50.  * Serialized objects of this class will not be compatible with 
  51.  * future Swing releases.  The current serialization support is appropriate
  52.  * for short term storage or RMI between applications running the same
  53.  * version of Swing.  A future release of Swing will provide support for
  54.  * long term persistence.
  55.  *
  56.  * @beaninfo
  57.  *   attribute: isContainer false
  58.  *
  59.  * @version 1.38 08/28/98
  60.  * @author Georges Saab
  61.  * @author David Karlton
  62.  */
  63. public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
  64.         Accessible 
  65. {
  66.     /**
  67.      * @see #getUIClassID
  68.      * @see #readObject
  69.      */
  70.     private static final String uiClassID = "CheckBoxMenuItemUI";
  71.  
  72.     /**
  73.      * Creates an initially unselected checkboxMenuItem with no set text or icon.
  74.      */
  75.     public JCheckBoxMenuItem() {
  76.         this(null, null, false);
  77.     }
  78.  
  79.     /**
  80.      * Creates an initially unselected checkboxMenuItem with an icon.
  81.      *
  82.      * @param icon the icon of the CheckBoxMenuItem.
  83.      */
  84.     public JCheckBoxMenuItem(Icon icon) {
  85.         this(null, icon, false);
  86.     }
  87.  
  88.     /**
  89.      * Creates an initially unselected checkboxMenuItem with text.
  90.      *
  91.      * @param text the text of the CheckBoxMenuItem
  92.      */
  93.     public JCheckBoxMenuItem(String text) {
  94.         this(text, null, false);
  95.     }
  96.     
  97.     /**
  98.      * Creates an initially unselected checkboxMenuItem with the specified text and icon.
  99.      *
  100.      * @param text the text of the CheckBoxMenuItem
  101.      * @param icon the icon of the CheckBoxMenuItem
  102.      */
  103.     public JCheckBoxMenuItem(String text, Icon icon) {
  104.     this(text, icon, false);
  105.     }
  106.  
  107.     /**
  108.      * Creates a checkboxMenuItem with the specified text and selection state.
  109.      *
  110.      * @param text the text of the CheckBoxMenuItem.
  111.      * @param b the selected state of the checkboxmenuitem
  112.      */
  113.     public JCheckBoxMenuItem(String text, boolean b) {
  114.         this(text, null, b);
  115.     }
  116.  
  117.     /**
  118.      * Creates a checkboxMenuItem with the specified text, icon, and selection state.
  119.      *
  120.      * @param text the text of the CheckBoxMenuItem
  121.      * @param icon the icon of the CheckBoxMenuItem
  122.      * @param b the selected state of the checkboxmenuitem
  123.      */
  124.     public JCheckBoxMenuItem(String text, Icon icon, boolean b) {
  125.         setModel(new JToggleButton.ToggleButtonModel());
  126.         init(text, icon);
  127.         setBorderPainted(false);
  128.         setFocusPainted(false);
  129.         setHorizontalTextPosition(RIGHT);
  130.         setHorizontalAlignment(LEFT);
  131.         setSelected(b);
  132.         updateUI();
  133.     }
  134.  
  135.     protected void init(String text, Icon icon) {
  136.         if(text != null) {
  137.             setText(text);
  138.             
  139.             if(icon != null) {
  140.                 setVerticalTextPosition(BOTTOM);
  141.             } 
  142.         }
  143.         
  144.         if(icon != null) {
  145.             setIcon(icon);
  146.         }
  147.         
  148.         // Listen for Focus events
  149.         addFocusListener(
  150.             new FocusListener() {
  151.             public void focusGained(FocusEvent event) {}
  152.             public void focusLost(FocusEvent event) {
  153.                 // When focus is lost, repaint if 
  154.                 // we focus information is painted
  155.                 if(isFocusPainted()) {
  156.                     repaint();
  157.                 }
  158.             }
  159.         }
  160.         );
  161.     }
  162.         
  163.     /**
  164.      * Notification from the UIFactory that the L&F
  165.      * has changed. 
  166.      *
  167.      * @see JComponent#updateUI
  168.      */
  169.     public void updateUI() {
  170.         setUI((MenuItemUI)UIManager.getUI(this));
  171.     }
  172.  
  173.  
  174.     /**
  175.      * Returns the name of the L&F class
  176.      * that renders this component.
  177.      *
  178.      * @return "CheckBoxMenuItemUI"
  179.      * @see JComponent#getUIClassID
  180.      * @see UIDefaults#getUI
  181.      */
  182.     public String getUIClassID() {
  183.         return uiClassID;
  184.     }
  185.             
  186.      /**
  187.       * Returns the selected-state of the item. This method
  188.       * exists for AWT compatibility only.  New code should
  189.       * use isSelected() instead.
  190.       *
  191.       * @return true  if the item is selected
  192.       */
  193.     public boolean getState() {
  194.         return isSelected();
  195.     }
  196.             
  197.     /**
  198.      * Sets the selected-state of the item. This method
  199.      * exists for AWT compatibility only.  New code should
  200.      * use setSelected() instead.
  201.      *
  202.      * @param b  a boolean value indicating the item's
  203.      *           selected-state, where true=selected
  204.      * @beaninfo
  205.      * description: The selection state of the Checkbox menu item
  206.      *      hidden: true
  207.      */
  208.     public synchronized void setState(boolean b) {
  209.         setSelected(b);
  210.     }
  211.             
  212.             
  213.     /**
  214.      * Returns an array (length 1) containing the checkbox menu item 
  215.      * label or null if the checkbox is not selected.
  216.      *
  217.      * @return an array containing 1 Object -- the text of the menu item
  218.      *         -- if the item is selected, otherwise null 
  219.      */
  220.     public synchronized Object[] getSelectedObjects() {
  221.         if (isSelected() == false)
  222.             return null;
  223.         Object[] selectedObjects = new Object[1];
  224.         selectedObjects[0] = getText();
  225.         return selectedObjects;
  226.     }
  227.  
  228.     /**
  229.      * Override <code>JComponent.requestFocus()</code> to prevent grabbing the focus.
  230.      */
  231.     public void requestFocus() {}
  232.  
  233.  
  234.  
  235.     /** 
  236.      * See readObject() and writeObject() in JComponent for more 
  237.      * information about serialization in Swing.
  238.      */
  239.     private void writeObject(ObjectOutputStream s) throws IOException {
  240.         s.defaultWriteObject();
  241.     if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  242.         ui.installUI(this);
  243.     }
  244.     }
  245.  
  246.  
  247.     /**
  248.      * Returns a string representation of this JCheckBoxMenuItem. This method 
  249.      * is intended to be used only for debugging purposes, and the 
  250.      * content and format of the returned string may vary between      
  251.      * implementations. The returned string may be empty but may not 
  252.      * be <code>null</code>.
  253.      * <P>
  254.      * Overriding paramString() to provide information about the
  255.      * specific new aspects of the JFC components.
  256.      * 
  257.      * @return  a string representation of this JCheckBoxMenuItem.
  258.      */
  259.     protected String paramString() {
  260.     return super.paramString();
  261.     }
  262.  
  263. /////////////////
  264. // Accessibility support
  265. ////////////////
  266.  
  267.     /**
  268.      * Get the AccessibleContext associated with this JComponent
  269.      *
  270.      * @return the AccessibleContext of this JComponent
  271.      */
  272.     public AccessibleContext getAccessibleContext() {
  273.         if (accessibleContext == null) {
  274.             accessibleContext = new AccessibleJCheckBoxMenuItem();
  275.         }
  276.         return accessibleContext;
  277.     }
  278.  
  279.     /**
  280.      * The class used to obtain the accessible role for this object.
  281.      * <p>
  282.      * <strong>Warning:</strong>
  283.      * Serialized objects of this class will not be compatible with
  284.      * future Swing releases.  The current serialization support is appropriate
  285.      * for short term storage or RMI between applications running the same
  286.      * version of Swing.  A future release of Swing will provide support for
  287.      * long term persistence.
  288.      */
  289.     protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem {
  290.         /**
  291.          * Get the role of this object.
  292.          *
  293.          * @return an instance of AccessibleRole describing the role of the 
  294.          * object
  295.          */
  296.         public AccessibleRole getAccessibleRole() {
  297.             return AccessibleRole.CHECK_BOX;
  298.         }
  299.     } // inner class AccessibleJCheckBoxMenuItem
  300. }
  301.